package com.fy.fyspace.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fy.fyspace.model.entity.FileShare;
import com.fy.fyspace.model.vo.file.FileShareResultVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FileShareMapper extends BaseMapper<FileShare> {
    
    /**
     * 联合查询分享列表（支持文件名模糊搜索，配合PageHelper分页）
     * @param userId 用户ID
     * @param keyword 文件名关键词
     * @return 分享列表
     */
    @Select({
        "<script>",
        "SELECT ",
        "    fs.shareId, fs.fileId, fs.userId, fs.validType, fs.code, ",
        "    DATE_FORMAT(fs.shareTime, '%Y-%m-%d %H:%i:%s') as shareTime, ",
        "    DATE_FORMAT(fs.expireTime, '%Y-%m-%d %H:%i:%s') as expireTime, ",
        "    fs.showCount, ",
        "    fi.fileName, fi.folderType, fi.fileType, fi.fileCover, ",
        "    fi.fileSize, fi.fileCategory ",
        "FROM fileShare fs ",
        "LEFT JOIN fileInfo fi ON fs.fileId = fi.fileId ",
        "WHERE fs.userId = #{userId} ",
        "<if test='keyword != null and keyword != \"\"'>",
        "    AND fi.fileName LIKE CONCAT('%', #{keyword}, '%') ",
        "</if>",
        "ORDER BY fs.shareTime DESC",
        "</script>"
    })
    List<FileShareResultVO> selectShareListWithFileInfo(
            @Param("userId") Long userId,
            @Param("keyword") String keyword
    );
} 